package com.fuhailiu.opengl.utils;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;

import com.fuhailiu.opengl.define.MSize;

public class DensityUtil {

	private static final String TAG = DensityUtil.class.getSimpleName();

	public static int dp2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	public static int px2dp(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	public static int sp2px(Context context, float spValue) {
		final float scale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * scale + 0.5f);
	}

	public static int px2sp(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (pxValue / scale + 0.5f);
	}

	public static MSize getScreenSize(Activity activity) {
		LogUtil.LogD(TAG, "getScreenSize   <---");

		if (null == activity) {
			return null;
		}

		DisplayMetrics metric = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
		int width = metric.widthPixels;
		int height = metric.heightPixels;

		LogUtil.LogD(TAG, "getScreenSize   width = " + width + ", height = " + height);
		LogUtil.LogD(TAG, "getScreenSize   --->");

		return new MSize(width, height);
	}

}
